export default {
  methods: {
    // 删除条款
    deleteClause(data) {
      let indexs = data.indexs;
      if (indexs.length === 1) {
        let list = Object.assign([], this.contractList);
        list.splice(indexs[0], 1);
        this.contractList = list;

        this.historyRecord('delete', data.indexsToString, data.value);
      } else if (indexs.length === 2) {
        let list = Object.assign([], this.contractList[indexs[0]].children);
        list.splice(indexs[1], 1);
        this.contractList[indexs[0]].children = list;

        this.historyRecord('delete', data.indexsToString, data.value);
      } else {
        console.error('超限！'); // eslint-disable-line
      }
    },

    // 编辑条款
    editClause(data) {
      let indexs = data.indexs;
      let clause = {};
      if (indexs.length === 1) {
        clause = this.getClause(this.contractList[indexs[0]]);

        this.historyRecord('edit', data.indexsToString, data.value);
      } else if (indexs.length === 2) {
        clause = this.getClause(this.contractList[indexs[0]].children[indexs[1]]);

        this.historyRecord('edit', data.indexsToString, data.value);
      } else {
        console.error('超限！'); // eslint-disable-line
      }

      this.clauseTitle = `编辑 ${data.indexsToString} 条款`;
      this.clauseIndexs = indexs;
      this.clause = clause;
      this.$refs.clauseEdit.dialogToggle(true);
    },
    /**
     * 获取去除 children属性 的条款对象
     * @param {Object} data 条款对象
     */
    getClause(data) {
      let clause = {};
      for (const key in data) {
        if (key === 'children') {
          continue;
        }
        clause[key] = data[key];
      }
      return clause;
    },

    // 保存编辑的条款
    editClauseChange(clause) {
      let indexs = this.clauseIndexs;
      if (indexs.length === 1) {
        this.contractList[indexs[0]].text = clause.text;
      } else if (indexs.length === 2) {
        this.contractList[indexs[0]].children[indexs[1]].text = clause.text;
      } else {
        console.error('超限！'); // eslint-disable-line
      }
      this.$refs.clauseEdit.dialogToggle(false);
    },

    // 新增条款
    addClause(data) {
      this.historyRecord('add', '', data.value);
    },

    /**
     * 记录操作轨迹
     * @param {String} actionType 操作类型
     * @param {String} indexsToString 条款序号
     * @param {Object} item 条款对象
     */
    historyRecord(actionType, indexsToString, item) {
      item = this.getClause(item);
      item.timestamp = new Date();
      item.actionType = actionType;
      item.indexsToString = indexsToString;
      this.historyList.push(item);
    }
  }
};
